package com.tianshuo.spirngfactories;

import com.tianshuo.spirngfactories.model.Task;
import com.tianshuo.spirngfactories.service.TaskProcess;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.SpringFactoriesLoader;

import java.util.List;

/**
 * @ClassName : AnnotationApplicationContextTest
 * @Description : 模拟的springboot通过SpringFactoriesLoader.loadFactories()加载META-INF/spring.factories文件
 * @Author : tianshuo
 * @Date: 2020-08-20 11:24
 */
@ComponentScan
//@Configuration
public class SpringFactoriesAnnotationApplicationContextTest {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(SpringFactoriesAnnotationApplicationContextTest.class);
        Task bean = applicationContext.getBean(Task.class);
        System.out.println(bean);

        List<TaskProcess> taskProcesses = SpringFactoriesLoader.loadFactories(TaskProcess.class,
                SpringFactoriesAnnotationApplicationContextTest.class.getClassLoader());

        for (TaskProcess t : taskProcesses) {
            t.process();
            System.out.println(t);
        }

//        TaskProcess bean1 = applicationContext.getBean(TaskProcess.class);
//        System.out.println(bean1);
    }
}

